package cn.tedu.service;

import cn.tedu.dao.OrderMapper;
import cn.tedu.entity.Order;
import cn.tedu.entity.OrderItem;
import cn.tedu.service.ex.OrderCreationExcption;
import cn.tedu.service.port.OrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service("oderService")
public class OderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    /**
     * 创建订单
     * @param order         订单记录信息对象
     * @param orderItems    订单商品对象
     * @throws OrderCreationExcption    自定义商品异常抛出则执行事物回滚
     */
    @Transactional
    public void createOrder(Order order, List<OrderItem> orderItems)throws OrderCreationExcption {
        Integer a = orderMapper.insertOrder(order);
        if (a != 1){
            throw new OrderCreationExcption("创建订单失败");
        }
        for (OrderItem item:orderItems) {
            a = orderMapper.insertOrderItem(item);
            if (a != 1){
                throw new OrderCreationExcption("创建订单失败");
            }
        }
    }
}
